package cn.iocoder.yudao.module.dify.controller.admin.translationtaskrecords;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.dify.controller.admin.translationtaskrecords.vo.TranslationTaskRecordsPageReqVO;
import cn.iocoder.yudao.module.dify.controller.admin.translationtaskrecords.vo.TranslationTaskRecordsRespVO;
import cn.iocoder.yudao.module.dify.controller.admin.translationtaskrecords.vo.TranslationTaskRecordsSaveReqVO;
import cn.iocoder.yudao.module.dify.dal.dataobject.translationtaskrecords.TranslationTaskRecordsDO;
import cn.iocoder.yudao.module.dify.service.translationtaskrecords.TranslationTaskRecordsService;
import io.github.imfangs.dify.client.exception.DifyApiException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - dify翻译工作流任务记录")
@RestController
@RequestMapping("/dify/translation-task-records")
@Validated
public class TranslationTaskRecordsController {

    @Resource
    private TranslationTaskRecordsService translationTaskRecordsService;

    @PutMapping("/update")
    @Operation(summary = "更新dify翻译工作流任务记录")
    @PreAuthorize("@ss.hasPermission('dify:translation-task-records:update')")
    public CommonResult<Boolean> updateTranslationTaskRecords(@Valid @RequestBody TranslationTaskRecordsSaveReqVO updateReqVO) {
        translationTaskRecordsService.updateTranslationTaskRecords(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除dify翻译工作流任务记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dify:translation-task-records:delete')")
    public CommonResult<Boolean> deleteTranslationTaskRecords(@RequestParam("id") Long id) {
        translationTaskRecordsService.deleteTranslationTaskRecords(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得dify翻译工作流任务记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dify:translation-task-records:query')")
    public CommonResult<TranslationTaskRecordsRespVO> getTranslationTaskRecords(@RequestParam("id") Long id) {
        TranslationTaskRecordsDO translationTaskRecords = translationTaskRecordsService.getTranslationTaskRecords(id);
        return success(BeanUtils.toBean(translationTaskRecords, TranslationTaskRecordsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得dify翻译工作流任务记录分页")
    @PreAuthorize("@ss.hasPermission('dify:translation-task-records:query')")
    public CommonResult<PageResult<TranslationTaskRecordsRespVO>> getTranslationTaskRecordsPage(@Valid TranslationTaskRecordsPageReqVO pageReqVO) {
        PageResult<TranslationTaskRecordsDO> pageResult = translationTaskRecordsService.getTranslationTaskRecordsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TranslationTaskRecordsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出dify翻译工作流任务记录 Excel")
    @PreAuthorize("@ss.hasPermission('dify:translation-task-records:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTranslationTaskRecordsExcel(@Valid TranslationTaskRecordsPageReqVO pageReqVO,
                                                  HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TranslationTaskRecordsDO> list = translationTaskRecordsService.getTranslationTaskRecordsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "dify翻译工作流任务记录.xls", "数据", TranslationTaskRecordsRespVO.class,
                BeanUtils.toBean(list, TranslationTaskRecordsRespVO.class));
    }

}